const uploadFile = (promise, config) => {
  return new Promise((resolve, reject) => {
    if (['produce', 'dev', 'host'].includes(config.env)) {
      var fileUrl = [];
      promise.filePaths.forEach(item => {
        wx.uploadFile({
          filePath: item.tempFilePath,
          name: 'file',
          url: config.api[config.env].url + promise.url,
          success: res => {
            res = JSON.parse(res.data);
            fileUrl.push(res.data);
            if (fileUrl.length >= promise.filePaths.length) {
              resolve(fileUrl);
            }
          },
          fail: err => {
            wx.showToast({ title: "网络异常", icon: 'none' });
            reject(err);
          }
        });
      });
    } else {
      wx.showToast({ title: "环境异常", icon: 'none' });
      reject({ msg: "环境异常" })
    }
  });
}

module.exports = {
  uploadFile
};